package com.wrxdz.start.surfaceviewt.surfview.utils;

import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;

public class PukeClickUtils {
    /**
     * 判断触摸点是否在一组纸牌内
     *
     * @param points 所有手牌
     * @param event  事件
     * @param a      覆盖值
     * @return 纸牌下标
     */
    public static int isTouchPointInPukes(Rect[] points, MotionEvent event, int a) {
        Rect[] points_ = new Rect[points.length];
        for (int i = 0; i < points.length; i++) {
            if (i == points.length - 1) {
                break;
            }
            points_[i] = new Rect(points[i].left, points[i].top, points[i].right - a, points[i].bottom);
        }
        for (int i = 0; i < points_.length; i++) {
            if (isTouchPointInPuke(points[i], event)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 判断触摸点是否在一张纸牌上
     *
     * @param point 某张纸牌
     * @param event 事件
     * @return 是否在纸牌上
     */
    private static boolean isTouchPointInPuke(Rect point, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        return x >= point.left && x <= point.right && y > point.top && y <= point.bottom;
    }

    /**
     * 判断触摸点是否点击按钮
     * @param points 坐标集
     * @param event 事件
     * @return 0出牌 1不出牌
     */
    public static int isTouchButton(RectF[] points, MotionEvent event) {
        int i = -1;
        int x = (int) event.getX();
        int y = (int) event.getY();
        for (int j = 0; j < points.length; j++) {
            i = x >= points[j].left && x <= points[j].right && y > points[j].top && y <= points[j].bottom ? j : -1;
            if (i == j) {
                return i;
            }
        }
        return i;
    }
}
